$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'service/servicerefunds/list',
        datatype: "json",
        colModel: [
			{label: '操作', name: 'srId', width: 100,
                formatter: function (value, options, row) {
                    if(row.srRefundStatus === 2){
                        return '<a href="javascript:;" onclick="return vm.confirmRefund('+ row.srSrId +');">确认退款</a></li>';
                    }else {
                    	return '';
					}
                }
			},
			{ label: 'srId', name: 'srId', index: 'sr_id', width: 50, key: true },
			{ label: '关联售后id', name: 'srSrId', index: 'sr_sr_id', width: 80 }, 			
			{ label: '退款单状态', name: 'srRefundStatusText', index: 'sr_refund_status', width: 80 },
			{ label: '退款时间', name: 'srRefundTime', index: 'sr_refund_time', width: 80 }, 			
			/*{ label: '退款人', name: 'srRefundName', index: 'sr_refund_name', width: 80 }, */
			{ label: '退款说明', name: 'srExplain', index: 'sr_explain', width: 80 }, 			
			{ label: '退款路径', name: 'srRefundPath', index: 'sr_refund_path', width: 80,
                formatter: function (value, options, row) {
                    var valueText = "原路退回";
                    if(value === '1'){
                        valueText = '其它';
					}
                    return valueText;
                }
			},
			{ label: '退款金额', name: 'srRefundAmount', index: 'sr_refund_amount', width: 80 }, 			
			{ label: '账户类型', name: 'srAccountType', index: 'sr_account_type', width: 80 ,
                formatter: function (value, options, row) {
                    var valueText = "";
                    if(value === '1'){
                        valueText = '银行卡';
                    }else if(value === '2'){
                    	valueText = '支付宝';
					}
                    return valueText;
                }
			},
			{ label: '银行卡号', name: 'srBankCode', index: 'sr_bank_code', width: 80 }, 			
			{ label: '持卡人姓名', name: 'srBankCardholder', index: 'sr_bank_cardholder', width: 80 }, 			
			{ label: '银行名称', name: 'srBankName', index: 'sr_bank_name', width: 80 }, 			
			{ label: '开户行', name: 'srBankOpen', index: 'sr_bank_open', width: 80 }, 			
			{ label: '支付宝账号', name: 'srAlipayCode', index: 'sr_alipay_code', width: 80 }, 			
			{ label: '支付宝姓名', name: 'srAlipayName', index: 'sr_alipay_name', width: 80 }, 			
			{ label: '更新人', name: 'srUpdateName', index: 'sr_update_name', width: 80 },
			{ label: '更新时间', name: 'srUpdateTime', index: 'sr_update_time', width: 80 }, 			
			{ label: '创建人', name: 'srCreateName', index: 'sr_create_name', width: 80 },
			{ label: '创建时间', name: 'srCreateTime', index: 'sr_create_time', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

});


var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        dialogRefundVisible: false,
		title: null,
		serviceRefunds: {},
		currentData:{},// 当前行信息
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.serviceRefunds = {};
		},
		update: function (event) {
			var srId = getSelectedRow();
			if(srId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(srId)
		},
		saveOrUpdate: function (event) {
			var url = vm.serviceRefunds.srId == null ? "service/servicerefunds/save" : "service/servicerefunds/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.serviceRefunds),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var srIds = getSelectedRows();
			if(srIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "service/servicerefunds/delete",
                    contentType: "application/json",
				    data: JSON.stringify(srIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(srId){
			$.get(baseURL + "service/servicerefunds/info/"+srId, function(r){
                vm.serviceRefunds = r.serviceRefunds;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		//确认退款
        confirmRefund : function (srSrId) {
			// 获取退款相关信息
            $.get(baseURL + "service/servicerefunds/getConfirmRefundInfo/"+srSrId, function(r){
                vm.currentData = r.serviceRefundsConfirm;
                vm.dialogRefundVisible = true;
            });
        }
	}
});